using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
using UnityEngine.Timeline;

/// <summary>
/// 写在前面 , Dirtector 和 Playable 两者都有对该Behavior的操作权限 , 所以在阶段回调用两者都需要设置接口
/// 
/// </summary>

[Serializable]
public class VSBasicBehaviour : PlayableBehaviour, ISerializationCallbackReceiver
{
    [LabelText("类型")] public VSClipType clipType;

    [LabelText("实例数据")]
    public VAct_EntityHandler_Data EntityHandler_Data;
    [LabelText("动画数据")]
    public VAct_AnimationHandler_Data AnimationHandler_Data;
    [LabelText("空置域数据")]
    public VAct_VacancyHandler_Data VacancyHandler_Data;
    [LabelText("交互点数据")]
    public VAct_InteractHandler_Data InteractHandler_Data;

    [LabelText("中控数据")]
    public VAct_ControlHandler_Data SkillControlHandler_Data;
    [LabelText("音频数据")]
    public VAct_AudioHandler_Data AudioHandler_Data ;

    // [FoldoutGroup("Transform")]
    // [OnValueChanged("OdinSyncRotation2Record")]
    // public Quaternion initRotation;

    #region  DEBUG
    [FoldoutGroup("DEBUG")]
    [DisplayAsString(false)]
    public string bindTransformPath;
    [FoldoutGroup("DEBUG")]
    public GameObject runtimeInstance;
    [FoldoutGroup("DEBUG")]
    public Transform runtimePivot;
    [FoldoutGroup("DEBUG")]
    public bool runtimeOnDirector = false;
    #endregion

    #region lifeCycle
    // public VSBehaviourHandle curHandle;

    public VActHandlerBase curHandle;
    [HideInInspector] public VActProcessor processor;
    public void InitBehaviour(VActProcessor processor)
    {
        this.processor = processor;
        curHandle = CreateHandle(clipType);
    }

    public override void OnPlayableCreate(Playable playable)
    {
        if (!runtimeOnDirector)
        {
            curHandle.Init();
        }
    }
    public override void OnPlayableDestroy(Playable playable)
    {
        if (!runtimeOnDirector)
        {
            curHandle.Clear();
        }
    }
    public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        if (runtimeOnDirector)
        {
            curHandle.Init();
        }
    }
    // 只在Director中自动调用, Playable中则需要手动调用
    public override void OnBehaviourPause(Playable playable, FrameData info)
    {
        if (runtimeOnDirector)
        {
            curHandle.Clear();
        }
    }

    public override void PrepareFrame(Playable playable, FrameData info)
    {
        curHandle.PrepareFrame(playable, info);
    }
    #endregion

    public VActHandlerBase CreateHandle(VSClipType vSClipType)
    {
        return vSClipType switch
        {
            VSClipType.Animation => new VAct_AnimationHandler(this),
            VSClipType.Vacancy => new VAct_VacancyHandler(this),
            VSClipType.Entity => new VAct_EntityHandler(this),
            VSClipType.Interact => new VAct_InteractHandler(this),
            VSClipType.Control => new VAct_ControlHandler(this),
            VSClipType.Audio => new VAct_AudioHandler(this),
            _ => null
        };
    }
    public void OnBeforeSerialize()
    {
        if (EntityHandler_Data == null || EntityHandler_Data.rootTransform == null) return;
        if (processor == null) return;
        var bindTransform = EntityHandler_Data.rootTransform;
        bindTransformPath = TransformToProcessor(bindTransform, "");

        string TransformToProcessor(Transform curNode, string path)
        {
            if (processor.transform == curNode) return path;
            if (curNode.transform.parent == null) throw new Exception("没有找到目标Processor");
            path = $"{curNode.name}/" + path;
            return TransformToProcessor(curNode.parent, path);
        }
    }
    public void OnAfterDeserialize() { }
    private void OdinSyncRotation2Record()
    {
        if (runtimeInstance != null)
        {
            // runtimeInstance.transform.rotation = initRotation;
            runtimeInstance.transform.position = runtimePivot.position;
        }
    }

}


public enum VSClipType
{
    Animation = 0,
    Vacancy = 1,
    Entity = 2,
    Interact = 3,
    Control = 4,
    Audio = 5
    // Particle = 3,
    // ScriptCall,
    // Warehouse,
    // Camera,
    // Text,
    // InteractNode
}
